﻿<cfscript>
/** 
* @hint "acadmicBase - Course"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.courseListener" 
	output=false 
	depends="senateMathAdvice, graduateProgressAdvice, schemaPropertyAdvice, 
				sessionAdvice, datetimeAdvice, sequenceProgramSchemaModify, 
				sequenceGraduateProgress, sequenceGraduateCondition, sequenceGraduateCharge,
				sequenceProgramSchema" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "取消培养方案修订申请"
	* 
	* @event "MachII Event Object"
	*/
	public void function applyModifyCancel( required MachII.framework.Event event ) output=false {
		
		var signId = event.getArg("REQ");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			var sql = "	UPDATE
							t_schema_modify sc
							SET
							   sc.sign_status = 'C'
						WHERE
							sc.sign_id = :signId ";
			
			var queryObj = new Query(datasource = application.dnsMaster);
			
			queryObj.addParam(name="signId", value=signId, cfsqltype="cf_sql_char");
			
			queryObj.execute(sql=sql);
			
		}
		
	}
	
	/**
	* @hint "整批批准培养方案修订申请"
	* 
	* @event "MachII Event Object"
	*/
	public void function applyModifyBatchApprove( required MachII.framework.Event event ) output=false {
		
		var tchId = getsessionAdvice().getAutherUserID();
		
		var currentTS = getdatetimeAdvice().getCurrentDatetimeString();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "	UPDATE
						t_schema_modify s
					SET
						s.admin_user_id = :adminUserId ,
						s.sign_status = 'P',
						s.date_review = :dateReview ,
						s.date_valid = :dateValid 
					WHERE
						s.sign_status = 'W'";
						
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "dateValid", value = dateFormat(dateAdd('d', 10, now()), 'YYYYMMDD'), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateReview", value = currentTS, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "adminUserId", value = tchId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);
		}
		
	}
	
	/**
	* @hint "批准培养方案修订申请"
	* 
	* @event "MachII Event Object"
	*/
	public void function applyModifyApprove( required MachII.framework.Event event ) output=false {
		
		var signId = event.getArg("REQ");
		
		var tchId = getsessionAdvice().getAutherUserID();
		
		var currentTS = getdatetimeAdvice().getCurrentDatetimeString();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "	UPDATE
						t_schema_modify s
					SET
						s.admin_user_id = :adminUserId ,
						s.sign_status = 'P',
						s.date_review = :dateReview ,
						s.date_valid = :dateValid 
					WHERE
						s.sign_id = :signId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "dateValid", value = replace(event.getArg("DateValid"), "-", "", "ALL"), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateReview", value = currentTS, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "adminUserId", value = tchId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "signId", value = signId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
		}
		
	}
	
	
	/**
	* @hint "拒绝培养方案修订申请"
	* 
	* @event "MachII Event Object"
	*/
	public void function applyModifyReject( required MachII.framework.Event event ) output=false {
		
		var signId = event.getArg("REQ");
		
		var tchId = getsessionAdvice().getAutherUserID();
		
		var currentTS = getdatetimeAdvice().getCurrentDatetimeString();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "	UPDATE
						t_schema_modify s
					SET
						s.admin_user_id = :adminUserId ,
						s.sign_status = 'B',
						s.date_review = :dateReview ,
						s.date_valid = :dateValid 
					WHERE
						s.sign_id = :signId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "dateValid", value = "00000000", cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "dateReview", value = currentTS, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "adminUserId", value = tchId, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "signId", value = signId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
		}
		
	}
	
	
	
	/**
	* @hint "添加培养方案修订申请"
	* 
	* @event "MachII Event Object"
	*/
	public void function applyModifySign( required MachII.framework.Event event ) output=false {
		
		var sequenceProgramSchemaModify = getsequenceProgramSchemaModify();
		
		var grade = event.getArg("Edition");
		var subjectId = event.getArg("SubjectID");
		
		var tchId = getsessionAdvice().getAutherUserID();
		
		var currentTS = getdatetimeAdvice().getCurrentDatetimeString();
		
		var signId = sequenceProgramSchemaModify.getID();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sql = "	INSERT INTO 
							t_schema_modify(
								sign_id,
								grade,
								sbj_id,
								sign_reason,
								sign_user_id,
								admin_user_id,
								sign_status,
								date_created,
								date_valid,
								date_review
							)VALUES(
								:signId ,
								:grade ,
								:subjectId ,
								:changeReason ,
								:signUserId ,
								:adminUserId ,
								:signStatus ,
								:dateCreated ,
								:dateValid ,
								:dateReview 
							)";
			
			queryObj = new Query(datasource = application.dnsMaster);
		
			queryObj.addParam(name="signId", value=signId, cfsqltype="cf_sql_char");
			queryObj.addParam(name="grade", value=grade, cfsqltype="cf_sql_char");
			queryObj.addParam(name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="changeReason", value=event.getArg("ChangeReason"), cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="signUserId", value=tchId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="adminUserId", value=tchId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="signStatus", value="W", cfsqltype="cf_sql_char");
			queryObj.addParam(name="dateCreated", value=currentTS, cfsqltype="cf_sql_char");
			queryObj.addParam(name="dateValid", value="00000000", cfsqltype="cf_sql_char");
			queryObj.addParam(name="dateReview", value="00000000000000", cfsqltype="cf_sql_char");
			
			queryObj.execute(sql=sql);
			
			var passArgs = structNew();
			structInsert( passArgs, "REQ", signId, true );
			
			event.setArgs( passArgs );
			
		}
		
	}
	
	
	/**
	* @hint "删除培养方案"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteSchemaEntitys( required MachII.framework.Event event ) output=false {

		var grade = event.getArg("Edition");
		var subjectId = event.getArg("SubjectID");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			/* 删除培养方案课程内容 */
			var sql = "	DELETE FROM
							t_schema s
						WHERE
							s.grade = :grade 
							AND 
							(
								s.sbj_id = :subjectId 
								OR 
								s.sbj_id LIKE :subjectDirs 
							)";
			
			var queryObj = new Query(datasource = application.dnsMaster);
		
			queryObj.addParam(name="grade", value = grade, cfsqltype="cf_sql_char");
			queryObj.addParam(name="subjectId", value = subjectId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="subjectDirs", value = subjectId & "_", cfsqltype="cf_sql_char");
			queryObj.execute(sql=sql);
			
			
			/* 删除培养方案学分要求 */
			sql = "	DELETE FROM
						t_condition c
					WHERE
						c.con_id = :conditionId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name="conditionId", value = grade & left(subjectId, 5), cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			/* 删除收费标准 */
			sql = "	DELETE FROM
						t_charge c
					WHERE
						c.chg_id = :chargeId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name="chargeId", value = grade & left(subjectId, 5), cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			/* 删除培养方案学习进度要求 */
			sql = "	DELETE FROM
						t_progress p
					WHERE
						p.grade = :grade 
						AND
						(
							p.sbj_id = :subjectId 
							OR
							p.sbj_id LIKE :subjectDirs 
						)";
			queryObj.addParam(name="grade", value = grade, cfsqltype="cf_sql_char");
			queryObj.addParam(name="subjectId", value = subjectId, cfsqltype="cf_sql_varchar");
			queryObj.addParam(name="subjectDirs", value = subjectId & "_", cfsqltype="cf_sql_char");
			queryObj.execute(sql=sql);
		}
		
	}
	
	/**
	* @hint "按学院复制培养方案"
	* 
	* @event "MachII Event Object"
	*/
	public void function copyInstituteSchema( required MachII.framework.Event event ) output=false {
		
		var sequenceProgramSchema = getsequenceProgramSchema();
		var sequenceGraduateCondition = getsequenceGraduateCondition();
		var sequenceGraduateCharge = getsequenceGraduateCharge();
		var sequenceGraduateProgress = getsequenceGraduateProgress();
		
		var instituteId = event.getArg("TargetInstituteID");
		var srcGrade = event.getArg("srcGrade");
		var tagGrade = event.getArg("targetGrade");
		var sql = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 第一步 复制培养方案课程 */	
			sql = "SELECT  
					  	t_schema.term_index, 
						t_subject.term_start, 
						t_subject.sbj_direction, 
						t_schema.sbj_id, 
						t_schema.cid, 
						t_schema.prop_id, 
						t_schema.cna_id, 
						t_schema.shm_prop 
					FROM 
						t_schema 
						INNER JOIN t_subject ON t_subject.sbj_id = t_schema.sbj_id 
					WHERE 
						t_schema.grade = :grade 
						AND 
						t_subject.institute_id = :instituteId 
					ORDER BY 
						t_schema.shm_id  "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="grade", value=srcGrade, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="instituteId", value=instituteId, cfsqltype="cf_sql_varchar" );
			rs_schemaEntitys = queryObj.execute( sql=sql ).getResult();
			
			for ( var row=1; row LTE rs_schemaEntitys.recordCount; row++){ 
				
				var academicTermYear = getsenateMathAdvice().convertTermIdxToAcademicTermYear( rs_schemaEntitys["term_index"][row], tagGrade, rs_schemaEntitys["term_start"][row]);
				
				/* 生成培养方案流水号 */
				sequenceProgramSchema.setSubjectID(rs_schemaEntitys["sbj_id"][row]);
				sequenceProgramSchema.setGrade(tagGrade);
				sequenceProgramSchema.setSubjectDirection(rs_schemaEntitys["sbj_direction"][row]);
				sequenceProgramSchema.setCourseID(rs_schemaEntitys["cid"][row]);
				
				var id = sequenceProgramSchema.getID();
				
				sql = "SELECT 1
						FROM t_schema 
						WHERE shm_id = :shmId "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
				rs_entityExists = queryObj.execute( sql=sql ).getResult();
				
				if (rs_entityExists.recordCount EQ 0) {
					
					 sql = "INSERT INTO t_schema 	
								   (
									  shm_id,
									  grade,
									  sbj_id,
									  cid,
									  term_index,
									  prop_id,
									  cna_id,
									  academic_year,
									  academic_term,
									  shm_prop 
								  )
								  VALUES(
									  :schemaId, 
									  :grade, 
									  :subjectId, 
									  :courseId, 
									  :termIndex, 
									  :propId, 
									  :cnaId, 
									  :academicYear, 
									  :academicTerm, 
									  :shm_prop 
								  )";
					queryObj = new query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="schemaId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="subjectId", value=rs_schemaEntitys["sbj_id"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="courseId", value=rs_schemaEntitys["cid"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="termIndex", value=rs_schemaEntitys["term_index"][row], cfsqltype="cf_sql_char"  );
					queryObj.addParam( name="propId", value=rs_schemaEntitys["prop_id"][row], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="cnaId", value=rs_schemaEntitys["cna_id"][row], cfsqltype="cf_sql_char"  );
					queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="shm_prop", value=rs_schemaEntitys["shm_prop"][row], cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql ); 
				
				}
				else {
					
					sql = "UPDATE t_schema 	
							  SET 
								  term_index = :termIndex,
								  prop_id = :propId,
								  cna_id = :cnaId,
								  academic_year = :academicYear,
								  academic_term = :academicTerm,
								  shm_prop = :shm_prop 
							  WHERE 
								  shm_id = :schemaId ";
					queryObj = new query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="schemaId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="termIndex", value=rs_schemaEntitys["term_index"][row], cfsqltype="cf_sql_char"  );
					queryObj.addParam( name="propId", value=rs_schemaEntitys["prop_id"][row], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="cnaId", value=rs_schemaEntitys["cna_id"][row], cfsqltype="cf_sql_char"  );
					queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
					queryObj.addParam( name="shm_prop", value=rs_schemaEntitys["shm_prop"][row], cfsqltype="cf_sql_varchar" );
					queryObj.execute( sql=sql );
				}

			}
			
			/* 第二步 复制培养方案学分要求 */
			sql = "SELECT 
						t_condition.sbj_id, 
						t_condition.credit, 
						t_condition.credit_required, 
						t_condition.credit_optional, 
						t_condition.credit_relish, 
						t_subject.term_start, 
						t_condition.period, 
						t_condition.period_required, 
						t_condition.period_optional 
					FROM  
						t_condition 
						INNER JOIN t_subject ON t_subject.sbj_id = t_condition.sbj_id 
					WHERE 
						t_condition.grade = :grade 
						AND 
						t_subject.institute_id = :instituteId 
					ORDER BY 
						t_condition.con_id "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="grade", value=srcGrade, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="instituteId", value=instituteId, cfsqltype="cf_sql_varchar" );
			rs_conditionEntitys = queryObj.execute( sql=sql ).getResult();
			
			for (var i=1; i LTE rs_conditionEntitys.recordCount; i++){
				
				/* 生成专业计划号 */
				sequenceGraduateCondition.setgrade(tagGrade);
				sequenceGraduateCondition.setsubjectId(rs_conditionEntitys["sbj_id"][i]);
				
				id = sequenceGraduateCondition.getID();
				
				sql = "SELECT 1
						FROM t_condition 
						WHERE con_id = :conId "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="conId", value=id, cfsqltype="cf_sql_varchar" );
				rs_entityExists = queryObj.execute( sql=sql ).getResult();
				
				if (rs_entityExists.recordCount EQ 0) {
					
					/* 秋季招生 则建班为9月 春季招生为3月 */
					if (rs_conditionEntitys["term_start"][i] == "1") {
						shemaFound = tagGrade & "09";
					} 
					else {
						shemaFound = tagGrade & "03";
					}
					
					sql = "INSERT INTO t_condition 	
								   (
									  con_id,
									  grade,
									  sbj_id,
									  credit,
									  credit_required,
									  credit_optional,
									  credit_relish,
									  period,
									  period_required,
									  period_optional,
									  found_date 
								   )
								  VALUES(
									  :conditionId, 
									  :grade, 
									  :subjectId, 
									  :credit, 
									  :creditRequired, 
									  :creditOptional, 
									  :creditRelish, 
									  :period, 
									  :periodRequired, 
									  :periodOptional, 
									  :foundDate 
								  )";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="conditionId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="subjectId", value=rs_conditionEntitys["sbj_id"][i], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="credit", value=rs_conditionEntitys["credit"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditRequired", value=rs_conditionEntitys["credit_required"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditOptional", value=rs_conditionEntitys["credit_optional"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditRelish", value=rs_conditionEntitys["credit_relish"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="period", value=rs_conditionEntitys["period"][i], cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="periodRequired", value=rs_conditionEntitys["period_required"][i], cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="periodOptional", value=rs_conditionEntitys["period_optional"][i], cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="foundDate", value=shemaFound, cfsqltype="cf_sql_char" );
					queryObj.execute( sql=sql );
					
				}
				else {
					sql = "UPDATE t_condition 	
								SET 
									credit = :credit, 
									credit_required = :creditRequired, 
									credit_optional = :creditOptional, 
									credit_relish = :creditRelish,
									period = :period, 
									period_required = :periodRequired,
									period_optional = :periodOptional 
								WHERE 
									con_id = :conditionId ";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="conditionId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="credit", value=rs_conditionEntitys["credit"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditRequired", value=rs_conditionEntitys["credit_required"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditOptional", value=rs_conditionEntitys["credit_optional"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="creditRelish", value=rs_conditionEntitys["credit_relish"][i], cfsqltype="cf_sql_decimal", scale=1 );
					queryObj.addParam( name="period", value=rs_conditionEntitys["period"][i], cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="periodRequired", value=rs_conditionEntitys["period_required"][i], cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="periodOptional", value=rs_conditionEntitys["period_optional"][i], cfsqltype="cf_sql_integer" );
					queryObj.execute( sql=sql );
					
					
				}
				
			}
			
			/* 第三步 复制收费标准 */
			sql = "SELECT  
						t_charge.sbj_id, 
						t_charge.regular, 
						t_charge.resit, 
						t_charge.secondary, 
						t_charge.assist, 
						t_charge.advanced 
					FROM 
						t_charge 
						INNER JOIN t_subject ON t_subject.sbj_id = t_charge.sbj_id 
					WHERE 
						t_charge.grade = :grade 
						AND 
						t_subject.institute_id = :instituteId 
					ORDER BY 
						t_charge.chg_id  "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="grade", value=srcGrade, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="instituteId", value=instituteId, cfsqltype="cf_sql_varchar" );
			rs_chargeEntitys = queryObj.execute( sql=sql ).getResult();
			
			for ( row=1; row LTE rs_chargeEntitys.recordCount; row++){
				
				/* 生成专业计划号 */
				sequenceGraduateCharge.setgrade(tagGrade);
				sequenceGraduateCharge.setsubjectId(rs_chargeEntitys["sbj_id"][row]);
				
				id = sequenceGraduateCharge.getID();
				
				sql = "SELECT 1
						FROM t_charge 
						WHERE chg_id = :chgId "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="chgId", value=id, cfsqltype="cf_sql_varchar" );
				rs_entityExists = queryObj.execute( sql=sql ).getResult();
					
				if (rs_entityExists.recordCount EQ 0) {
					
					sql = "INSERT INTO t_charge(
									chg_id,
									grade,
									sbj_id,
									regular,
									resit,
									secondary,
									assist,
									advanced
                				   )
								  VALUES(
									:chgId, 
									:grade, 
									:subjectId, 
									:regular, 
									:resit, 
									:secondary, 
									:assist, 
									:advanced 	
								  )";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="chgId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="subjectId", value=rs_chargeEntitys["sbj_id"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="regular", value=rs_chargeEntitys["regular"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="resit", value=rs_chargeEntitys["resit"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="secondary", value=rs_chargeEntitys["secondary"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="assist", value=rs_chargeEntitys["assist"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="advanced", value=rs_chargeEntitys["advanced"][row], cfsqltype="cf_sql_decimal", scale=2 );
					
					queryObj.execute( sql=sql );
			
				}
				else {
					sql = "UPDATE t_charge
								SET 
									grade = :grade, 
									regular = :regular, 
									resit = :resit, 
									secondary = :secondary, 
									assist = :assist, 
									advanced = :advanced 
                				WHERE 
									chg_id = :chgId ";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="chgId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="regular", value=rs_chargeEntitys["regular"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="resit", value=rs_chargeEntitys["resit"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="secondary", value=rs_chargeEntitys["secondary"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="assist", value=rs_chargeEntitys["assist"][row], cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="advanced", value=rs_chargeEntitys["advanced"][row], cfsqltype="cf_sql_decimal", scale=2 );
					
					queryObj.execute( sql=sql );
				
				}

			}
			
			/* 第四步 复制学习进度要求 */
			sql = "SELECT  
						t_progress.sbj_id, 
						t_progress.credit_required, 
						t_progress.credit_all 
					FROM 
						t_progress 
						INNER JOIN t_subject ON t_subject.sbj_id = t_progress.sbj_id 
					WHERE 
						t_progress.grade = :grade 
						AND 
						t_subject.institute_id = :instituteId 
					ORDER BY 
						t_progress.pgs_id  "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="grade", value=srcGrade, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="instituteId", value=instituteId, cfsqltype="cf_sql_varchar" );
			rs_progressEntitys = queryObj.execute( sql=sql ).getResult();
			
			for ( row=1; row LTE rs_progressEntitys.recordCount; row++){
				
				/* 生成专业计划号 */
				sequenceGraduateProgress.setgrade(tagGrade);
				sequenceGraduateProgress.setsubjectId(rs_progressEntitys["sbj_id"][row]);
				
				id = sequenceGraduateProgress.getID();
				
				sql = "SELECT 1
						FROM t_progress 
						WHERE pgs_id = :pgsId "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="pgsId", value=id, cfsqltype="cf_sql_varchar" );
				rs_entityExists = queryObj.execute( sql=sql ).getResult();
				
				if (rs_entityExists.recordCount EQ 0) {
					
					sql = "INSERT INTO t_progress(
									pgs_id,
									grade,
									credit_required,
									credit_all,
									sbj_id
                				   )
								  VALUES(
									:pgsId, 
									:grade, 
									:creditRequired, 
									:creditAll, 
									:subjectId
								  )";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="pgsId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="creditRequired", value=rs_progressEntitys["credit_required"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditAll", value=rs_progressEntitys["credit_all"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="subjectId", value=rs_progressEntitys["sbj_id"][row], cfsqltype="cf_sql_varchar" );
										
					queryObj.execute( sql=sql );
					
					
				}
				else {
					sql = "UPDATE t_progress
								SET
									grade = :grade, 
									credit_required = :creditRequired, 
									credit_all = :creditAll 
                				WHERE  
									pgs_id = :pgsId ";
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="pgsId", value=id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=tagGrade, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="creditRequired", value=rs_progressEntitys["credit_required"][row], cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditAll", value=rs_progressEntitys["credit_all"][row], cfsqltype="cf_sql_varchar" );
										
					queryObj.execute( sql=sql );
					
				}
			
			
			}

		}
		
	}
	
	/**
	* @hint "复制专业培养方案"
	* 
	* @event "MachII Event Object"
	*/
	public void function copySchemaEntitys( required MachII.framework.Event event ) output=false {
		
		var sequenceProgramSchema = getsequenceProgramSchema();
			
		var sql = "";
		var subjectId = event.getArg("SubjectID");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT term_start 
					  FROM t_subject 
					  WHERE sbj_id = :subjectId "; 
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
			rs_subject = queryObj.execute( sql=sql ).getResult();

			/* 复制培养方案课程内容 */
			
			for (targetTerm in listToArray(event.getArg("Term"))) {
				for (targetProp in listToArray(event.getArg("CourseProperty"))) {
					
					sql = "SELECT 
								a.term_index, a.sbj_id, a.cid, 
								a.prop_id, a.cna_id, a.shm_prop,
								t_subject.sbj_direction 
							FROM 
								t_schema a 
								INNER JOIN t_subject ON t_subject.sbj_id = a.sbj_id 
							WHERE 
								a.grade = :grade 
								AND 
								( a.sbj_id = :subjectId OR a.sbj_id LIKE :subjectDirs )
								AND 
								a.term_index = :targetTerm 
								AND 
								a.prop_id = :targetProp 
							ORDER BY 
								a.shm_id "; 
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="grade", value=event.getArg("Edition"), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="subjectDirs", value=subjectId & "_", cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="targetTerm", value=targetTerm, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="targetProp", value=targetProp, cfsqltype="cf_sql_char" );
					rs_schemaEntitys = queryObj.execute( sql=sql ).getResult();

					for(var i=1; i lte rs_schemaEntitys.recordCount; i++){
						
						academicTermYear = getsenateMathAdvice().convertTermIdxToAcademicTermYear(rs_schemaEntitys["term_index"][i], event.getArg("targetEdition"), rs_subject.term_start);
						
						/* 生成培养方案流水号 */
						sequenceProgramSchema.setSubjectID(rs_schemaEntitys["sbj_id"][i]);
						sequenceProgramSchema.setGrade(event.getArg("targetEdition"));
						sequenceProgramSchema.setSubjectDirection(rs_schemaEntitys["sbj_direction"][i]);
						sequenceProgramSchema.setCourseID(rs_schemaEntitys["cid"][i]);
						
						id = sequenceProgramSchema.getID();
						
						sql = "SELECT 1 
								FROM t_schema 
								WHERE shm_id = :shmID "; 
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="shmID", value=id, cfsqltype="cf_sql_varchar" );
						rs_entity = queryObj.execute( sql=sql ).getResult();

						if (rs_entity.recordCount EQ 0) {
							
							sql = "INSERT INTO 
									   t_schema (
											shm_id,
											grade,
											sbj_id,
											cid,
											term_index,
											prop_id,
											cna_id,
											academic_year,
											academic_term,
											shm_prop ) 
										VALUES(
											:shmId,
											:grade,
											:subjectId,
											:cid, 
											:termIndex,
											:propId, 
											:cnaId,
											:academicYear,
											:academicTerm, 
											:schemaProps 
										) ";
											
							queryObj = new Query( datasource=application.dnsMaster );
						
							queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
							queryObj.addParam( name="subjectId", value=rs_schemaEntitys["sbj_id"][i], cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="cid", value=rs_schemaEntitys["cid"][i], cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="termIndex", value=rs_schemaEntitys["term_index"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="propId", value=rs_schemaEntitys["prop_id"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="cnaId", value=rs_schemaEntitys["cna_id"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="schemaProps", value=rs_schemaEntitys["shm_prop"][i], cfsqltype="cf_sql_varchar" );
							
							queryObj.execute( sql=sql );
							
						}
						else {
							
							sql = "UPDATE 
										t_schema 
									SET 
										term_index = :termIndex, 
										prop_id = :propId, 
										cna_id = :cnaId, 
										shm_prop = :shmProp, 
										academic_year = :academicYear, 
										academic_term = :academicTerm 
									WHERE 
										shm_id = :shmId "; 
							queryObj = new Query( datasource=application.dnsMaster );
							queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="termIndex", value=rs_schemaEntitys["term_index"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="propId", value=rs_schemaEntitys["prop_id"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="cnaId", value=rs_schemaEntitys["cna_id"][i], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
							queryObj.addParam( name="shmProp", value=rs_schemaEntitys["shm_prop"][i], cfsqltype="cf_sql_varchar" );
							
							queryObj.execute( sql=sql );
						}
					}
					
				}
			}
			
			/* 复制培养方案学分要求 */
			
			sql = "SELECT 
						credit, credit_required, credit_optional, 
						credit_relish, period, period_required, period_optional 
					FROM 
						t_condition 
					WHERE 
						con_id = :conId  "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="conId", value=event.getArg("Edition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
			rs_condition = queryObj.execute( sql=sql ).getResult();
	
			if (rs_condition.recordCount GT 0) {
				
				sql = "UPDATE 
							t_condition 
						SET 
							credit = :credit, 
							credit_required = :creditRequired, 
							credit_optional = :creditOptional, 
							credit_relish = :creditRelish, 
							period = :period, 
							period_required = :periodRequired, 
							period_optional = :periodOprional 
						WHERE  
							con_id = :conId "; 
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="conId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="credit", value=rs_condition.credit, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRequired", value=rs_condition.credit_required, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditOptional", value=rs_condition.credit_optional, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRelish", value=rs_condition.credit_relish, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="period", value=rs_condition.period, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodRequired", value=rs_condition.period_required, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodOprional", value=rs_condition.period_optional, cfsqltype="cf_sql_integer" );
				
				queryObj.execute( sql=sql );	

			}
			
			/* 复制收费标准 */
			
			sql = "SELECT 
						sbj_id, regular, resit, secondary,
						assist, advanced 
					FROM 
						t_charge 
					WHERE 
						chg_id = :chgId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="chgId", value=event.getArg("Edition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
			rs_charge = queryObj.execute( sql=sql ).getResult();

			if (rs_charge.recordCount GT 0) {
				
				
				sql = "SELECT 1 
						FROM t_charge 
						WHERE chg_id = :chgId "; 
						
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="chgId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
				rs_chargeToUpdate = queryObj.execute( sql=sql ).getResult();
	
				if (rs_chargeToUpdate.recordCount EQ 0) {
					
					sql = "INSERT INTO 
							   t_charge (
							   		chg_id, 
									grade, 
									sbj_id,
									regular,
									resit,
									secondary,
									assist,
									advanced 
								) 
								VALUES(
									:chgId, 
									:grade, 
									:sbjId, 
									:regular, 
									:resit, 
									:secondary, 
									:assist, 
									:advanced 
								) ";
					queryObj = new Query( datasource=application.dnsMaster );
				
					queryObj.addParam( name="chgId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="sbjId", value=rs_charge.sbj_id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="regular", value=rs_charge.regular, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="resit", value=rs_charge.resit, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="secondary", value=rs_charge.secondary, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="assist", value=rs_charge.assist, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="advanced", value=rs_charge.advanced, cfsqltype="cf_sql_decimal", scale=2 );

					queryObj.execute( sql=sql );
				
				}
				else {
					sql = "UPDATE 
								t_charge 
							SET 
								grade = :grade, 
								regular = :regular, 
								resit = :resit, 
								secondary = :secondary, 
								assist = :assist, 
								advanced = :advanced 
							WHERE 
								chg_id = :chgId "; 
					queryObj = new Query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="chgId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="regular", value=rs_charge.regular, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="resit", value=rs_charge.resit, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="secondary", value=rs_charge.secondary, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="assist", value=rs_charge.assist, cfsqltype="cf_sql_decimal", scale=2 );
					queryObj.addParam( name="advanced", value=rs_charge.advanced, cfsqltype="cf_sql_decimal", scale=2 );
					
					queryObj.execute( sql=sql );
	
				}
			}			
			
			/* 复制培养方案学习进度要求 */
			
			sql = "SELECT  
						credit_required, credit_all, sbj_id 
					FROM 
						t_progress 
					WHERE 
						pgs_id = :pgsId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="pgsId", value=event.getArg("Edition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
			rs_progress = queryObj.execute( sql=sql ).getResult();
	
			if (rs_progress.recordCount GT 0) {
				
				sql = "SELECT 1 
						FROM t_progress 
						WHERE pgs_id = :pgs_id "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="pgs_id", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
				rs_progressToUpdate = queryObj.execute( sql=sql ).getResult();
			
				if (rs_progressToUpdate.recordCount EQ 0) {
					
					sql = "INSERT INTO 
							   t_progress (
							  		pgs_id,
									grade,
									sbj_id,
									credit_required,
									credit_all 
								) 
								VALUES(
									:pgsId,
									:grade, 
									:sbjId, 
									:creditRequired, 
									:creditAll 
								) ";
					queryObj = new Query( datasource=application.dnsMaster );
				
					queryObj.addParam( name="pgsId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="sbjId", value=rs_progress.sbj_id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditRequired", value=rs_progress.credit_required, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditAll", value=rs_progress.credit_all, cfsqltype="cf_sql_varchar" );

					queryObj.execute( sql=sql );
					
				}
				else {
					sql = "UPDATE 
								t_progress 
							SET 
								grade = :grade, 
								credit_required = :creditRequired, 
								credit_all = :creditAll, 
								sbj_id = :sbjId 
							WHERE 
								pgs_id = :pgsId "; 
					queryObj = new Query( datasource=application.dnsMaster );
					
					queryObj.addParam( name="pgsId", value=event.getArg("targetEdition") & left(subjectId, 5), cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
					queryObj.addParam( name="sbjId", value=rs_progress.sbj_id, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditRequired", value=rs_progress.credit_required, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="creditAll", value=rs_progress.credit_all, cfsqltype="cf_sql_varchar" );
					
					queryObj.execute( sql=sql );
				}
			} 
			
			/* 复制培养方案学习进度要求到专业方向2 */
			sql = "SELECT sbj_id 
					FROM t_subject 
					WHERE sbj_id LIKE :parent 
					ORDER BY sbj_id ASC "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="parent", value=subjectId & '_', cfsqltype="cf_sql_varchar" );
			rs_dir = queryObj.execute( sql=sql ).getResult();

			if (rs_dir.recordCount) {
				for ( var i=1; i LTE rs_dir.recordCount; i++){
						
					sql = "SELECT credit_required, credit_all, sbj_id 
							FROM t_progress 
							WHERE pgs_id = :pgs_id "; 
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="pgs_id", value=event.getArg("Edition") & rs_dir.sbj_id, cfsqltype="cf_sql_varchar" );
					rs_progress = queryObj.execute( sql=sql ).getResult();

					if (rs_progress.recordCount) {
						
						sql = "SELECT 1 
								FROM t_progress 
								WHERE pgs_id = :pgs_id "; 
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="pgs_id", value=event.getArg("targetEdition") & rs_dir.sbj_id, cfsqltype="cf_sql_varchar" );
						rs_progressToUpdate = queryObj.execute( sql=sql ).getResult();
				
						if (rs_progressToUpdate.recordCount EQ 0) {
							sql = "INSERT INTO 
									   t_progress (
											pgs_id,
											grade,
											sbj_id,
											credit_required,
											credit_all 
										) 
										VALUES(
											:pgsId,
											:grade, 
											:sbjId, 
											:creditRequired, 
											:creditAll 
										) ";
							queryObj = new Query( datasource=application.dnsMaster );
						
							queryObj.addParam( name="pgsId", value=event.getArg("targetEdition") & rs_dir.sbj_id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
							queryObj.addParam( name="sbjId", value=rs_progress.sbj_id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="creditRequired", value=rs_progress.credit_required, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="creditAll", value=rs_progress.credit_all, cfsqltype="cf_sql_varchar" );
		
							queryObj.execute( sql=sql );
					
						}
						else {
							sql = "UPDATE 
										t_progress 
									SET 
										grade = :grade, 
										credit_required = :creditRequired, 
										credit_all = :creditAll, 
										sbj_id = :sbjId 
									WHERE 
										pgs_id = :pgsId "; 
							queryObj = new Query( datasource=application.dnsMaster );
							
							queryObj.addParam( name="pgsId", value=event.getArg("targetEdition") & rs_dir.sbj_id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="grade", value=event.getArg("targetEdition"), cfsqltype="cf_sql_char" );
							queryObj.addParam( name="sbjId", value=rs_progress.sbj_id, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="creditRequired", value=rs_progress.credit_required, cfsqltype="cf_sql_varchar" );
							queryObj.addParam( name="creditAll", value=rs_progress.credit_all, cfsqltype="cf_sql_varchar" );
							
							queryObj.execute( sql=sql );
						}
					}
					
				}
			}
			
			/* Pass Args */
			event.setArg("Edition", event.getArg("targetEdition"));
			event.setArg("SubID", left(subjectId, 5));
			event.setArg("Schema", event.getArg("targetEdition") & left(subjectId, 5));
			
		}
		
	}

	/**
	* @hint "更改单条专业课程授课方向"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateSchemaEntityDirection( required MachII.framework.Event event ) output=false {
		
		var sequenceProgramSchema = getsequenceProgramSchema();
		
		var id = "";
		var academicTermYear = [];
		var sql = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT 
						sbj_id, term_index, grade, cid, 
						prop_id, cna_id, shm_prop 
					FROM 
						t_schema 
					WHERE 
						shm_id = :shmId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			rs_entity = queryObj.execute( sql=sql ).getResult();
			
			sql = "SELECT 
						sbj_id, term_start, sbj_direction 
					FROM 
						t_subject 
					WHERE 
						sbj_id = :subjectId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="subjectId", value=event.getArg("SubjectDirection"), cfsqltype="cf_sql_varchar" );
			rs_subject = queryObj.execute( sql=sql ).getResult();
			
			/* 检查专业方向是否发生改变 */
			if ( rs_entity.sbj_id != rs_subject.sbj_id ) {

				academicTermYear = getsenateMathAdvice().convertTermIdxToAcademicTermYear(rs_entity.term_index, rs_entity.grade, rs_subject.term_start);
				
				
				/* 生成培养方案流水号 */
				sequenceProgramSchema.setSubjectID(rs_subject.sbj_id);
				sequenceProgramSchema.setGrade(rs_entity.grade);
				sequenceProgramSchema.setSubjectDirection(rs_subject.sbj_direction);
				sequenceProgramSchema.setCourseID(rs_entity.cid);
				
				id = sequenceProgramSchema.getID();
				
				sql = "SELECT 1 
						FROM t_schema 
						WHERE shm_id = :shmID "; 
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="shmID", value=id, cfsqltype="cf_sql_varchar" );
				rs_schema = queryObj.execute( sql=sql ).getResult();
				
				if(rs_schema.recordCount EQ 0){
					sql = "INSERT INTO 
							   t_schema (
									shm_id,
									grade,
									sbj_id,
									cid,
									term_index,
									prop_id,
									cna_id,
									academic_year,
									academic_term,
									shm_prop ) 
								VALUES(
									:shmId,
									:grade,
									:subjectId,
									:cid, 
									:termIndex,
									:propId, 
									:cnaId,
									:academicYear,
									:academicTerm, 
									:schemaProps 
								) ";
				}else{
					sql = "UPDATE t_schema 
							SET
								grade = :grade,
								sbj_id = :subjectId,
								cid = :cid,
								term_index = :termIndex,
								prop_id = :propId,
								cna_id = :cnaId,
								academic_year = :academicYear,
								academic_term = :academicTerm,
								shm_prop = :schemaProps 
							WHERE 
								shm_id = :shmId ";	
				}
								
				queryObj = new Query( datasource=application.dnsMaster );
			
				queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="grade", value=rs_entity.grade, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="subjectId", value=rs_subject.sbj_id, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="cid", value=rs_entity.cid, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="termIndex", value=rs_entity.term_index, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="propId", value=rs_entity.prop_id, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="cnaId", value=rs_entity.cna_id, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="schemaProps", value=rs_entity.shm_prop, cfsqltype="cf_sql_varchar" );
				
				queryObj.execute( sql=sql );
				
				/* Pass Args */
				event.setArg("Edition", rs_entity.grade);
				event.setArg("SubID", left(rs_subject.sbj_id, 5));
				event.setArg("Schema", rs_entity.grade & left(rs_subject.sbj_id, 5));
				event.setArg("Term", rs_entity.term_index);
				
				/* 专业方向信息发生了改变 */
				if (len(rs_subject.sbj_id) eq 6) {
					event.setArg("Dir", rs_subject.sbj_id);
				}
				else {
					event.setArg("Dir", rs_entity.sbj_id);
				}

				sql = "DELETE FROM
							t_schema 
						WHERE 
							shm_id = :shmId ";
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );
			
			}
			else {
			
				/* 如果专业没有变化 则忽略 */
			
				/* Pass Args */
				event.setArg("Edition", rs_entity.grade);
				event.setArg("SubID", left(rs_entity.sbj_id, 5));
				event.setArg("Schema", rs_entity.grade & left(rs_entity.sbj_id, 5));
				event.setArg("Term", rs_entity.term_index);
				
				event.setArg("Dir", rs_entity.sbj_id);
			
			}
			
		}
		
	}


	/**
	* @hint "设置专业辅修课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createAssistDegreeEntity( required MachII.framework.Event event ) output=false {
		/**毕业及学位*/ 
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			propAdvice.parseProperty( rs_schema.shm_prop );
			
			propAdvice.setAssistCourse( true );
			 
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}


	/**
	* @hint "取消专业辅修课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteAssistDegreeEntity( required MachII.framework.Event event ) output=false {
	 	/**毕业及学位*/ 
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			
			propAdvice.parseProperty( rs_schema.shm_prop );
			
			propAdvice.setAssistCourse( false );
			 
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}


	/**
	* @hint "设置专业学位课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createSecDegreeEntity( required MachII.framework.Event event ) output=false {
		/**毕业及学位*/ 
		var entity = "";
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			
			propAdvice.parseProperty( rs_schema.shm_prop );
			
			propAdvice.setSecondDegree( true );
			 
			
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}
	
	
	/**
	* @hint "取消专业双学位课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteSecDegreeEntity( required MachII.framework.Event event ) output=false {
		/**毕业及学位*/  
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			
			propAdvice.parseProperty( rs_schema.shm_prop );
			
			propAdvice.setSecondDegree( false );
			 
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}
	

	/**
	* @hint "设置专业学位课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createDegreeEntity( required MachII.framework.Event event ) output=false {
		/**毕业及学位*/  
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			
			propAdvice.parseProperty( rs_schema.shm_prop );
			
			propAdvice.setDegreeCourse( true );
			 
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}
	
	/**
	* @hint "取消专业学位课程信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteDegreeEntity( required MachII.framework.Event event ) output=false {
		/**毕业及学位*/  
		var propAdvice = getschemaPropertyAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			 
			var sql = "SELECT a.shm_prop 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schema = queryObj.execute( sql=sql ).getResult();
			
			propAdvice.parseProperty( rs_schema.shm_prop);
			
			propAdvice.setDegreeCourse( false );
			 
			sql = "UPDATE t_schema a 
					SET 
						a.shm_prop = : schProp 
					WHERE 
						a.shm_id = :SchemaID ";
			var updateObj = new Query(datasource = application.dnsMaster);
			updateObj.addParam(name ="schProp", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar");
			updateObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			updateObj.execute(sql = sql);
			
			
			sql = "SELECT a.grade,a.sbj_id 
						FROM t_schema a 
						WHERE a.shm_id = :SchemaID ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="SchemaID", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			var rs_schemas = queryObj.execute( sql=sql ).getResult();
			/* Pass Args */
			event.setArg("Edition", rs_schemas.grade);
			event.setArg("SubID", left(rs_schemas.sbj_id, 5));
			event.setArg("Schema", rs_schemas.grade & left(rs_schemas.sbj_id, 5));
			
		}
		
	}
	
	/**
	* @hint "添加单条专业课程设置信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createSchemaEntity( required MachII.framework.Event event ) output=false {
		
		var sequenceProgramSchema = getsequenceProgramSchema();

		var id = "";
		var academicTermYear = [];
		var propAdvice = getschemaPropertyAdvice();
		var sql = "";

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT 
						sbj_id, term_start, sbj_direction 
					FROM 
						t_subject 
					WHERE 
						sbj_id = :subjectId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="subjectId", value=event.getArg("SubjectID"), cfsqltype="cf_sql_varchar" );
			rs_subject = queryObj.execute( sql=sql ).getResult();
			
			academicTermYear = getsenateMathAdvice().convertTermIdxToAcademicTermYear(event.getArg("Term"), event.getArg("Grade"), rs_subject.term_start);
		
			/* 生成培养方案流水号 */
			sequenceProgramSchema.setSubjectID(rs_subject.sbj_id);
			sequenceProgramSchema.setGrade(event.getArg("Grade"));
			sequenceProgramSchema.setSubjectDirection(rs_subject.sbj_direction);
			sequenceProgramSchema.setCourseID(event.getArg("CourseID"));
			
			id = sequenceProgramSchema.getID();
	
			/* Pass Args */
			event.setArg("Edition", event.getArg("Grade"));
			event.setArg("SubID", left(rs_subject.sbj_id, 5));
			event.setArg("Schema", event.getArg("Grade") & left(rs_subject.sbj_id, 5));
			event.setArg("Term", event.getArg("Term"));
			
			event.setArg("Dir", rs_subject.sbj_id);
	
			sql = "SELECT 1
					FROM t_schema 
					WHERE shm_id = :shmId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
			rs_entity = queryObj.execute( sql=sql ).getResult();
			
			if(rs_entity.recordCount EQ 0){
				sql = "INSERT INTO 
						   t_schema (
								shm_id,
								grade,
								sbj_id,
								cid,
								term_index,
								prop_id,
								cna_id,
								academic_year,
								academic_term,
								shm_prop ) 
							VALUES(
								:shmId,
								:grade,
								:subjectId,
								:cid, 
								:termIndex,
								:propId, 
								:cnaId,
								:academicYear,
								:academicTerm, 
								:schemaProps 
							) ";
			}else{
				sql = "UPDATE t_schema 
						SET
							grade = :grade,
							sbj_id = :subjectId,
							cid = :cid,
							term_index = :termIndex,
							prop_id = :propId,
							cna_id = :cnaId,
							academic_year = :academicYear,
							academic_term = :academicTerm,
							shm_prop = :schemaProps 
						WHERE 
							shm_id = :shmId ";
			}
							
			queryObj = new Query( datasource=application.dnsMaster );
		
			queryObj.addParam( name="shmId", value=id, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="grade", value=event.getArg("Grade"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="subjectId", value=rs_subject.sbj_id, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cid", value=event.getArg("CourseID"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="termIndex", value=event.getArg("Term"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="propId", value=event.getArg("CourseProperty"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cnaId", value=event.getArg("CourseNature"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
			queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
			queryObj.addParam( name="schemaProps", value=propAdvice.stringProperty(), cfsqltype="cf_sql_varchar" );
			
			queryObj.execute( sql=sql );
			
			
		}
		
	}
	
	/**
	* @hint "删除单条专业课程设置信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteSchemaEntity( required MachII.framework.Event event ) output=false {

		var sql = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT grade, sbj_id, term_index 
					FROM t_schema 
					WHERE shm_id = :shmId ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			rs_entity = queryObj.execute( sql=sql ).getResult();
			
			/* Pass Args */
			event.setArg("Edition", rs_entity.grade);
			event.setArg("SubID", left(rs_entity.sbj_id, 5));
			event.setArg("Schema", rs_entity.grade & left(rs_entity.sbj_id, 5));
			event.setArg("Term", rs_entity.term_index);
			
			event.setArg("Dir", rs_entity.sbj_id);
			
			sql = "DELETE FROM t_schema 
					WHERE shm_id = :shmId ";
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			queryObj.execute( sql=sql );

		}
		
	}
	
	/**
	* @hint "更新单条专业课程设置信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateSchemaEntity( required MachII.framework.Event event ) output=false {

		var academicTermYear = [];
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "SELECT 
					    t_schema.grade, t_subject.term_start, 
					    t_schema.sbj_id, t_schema.term_index  
					FROM 
						t_schema 
						INNER JOIN t_subject ON t_subject.sbj_id = t_schema.sbj_id 
					WHERE 
						t_schema.shm_id = :shmId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			rs_entity = queryObj.execute( sql=sql ).getResult();
			
			academicTermYear = getsenateMathAdvice().convertTermIdxToAcademicTermYear(event.getArg("Term"), rs_entity.grade, rs_entity.term_start);
	
			/* Pass Args */
			event.setArg("Edition", rs_entity.grade);
			event.setArg("SubID", left(rs_entity.sbj_id, 5));
			event.setArg("Schema", rs_entity.grade & left(rs_entity.sbj_id, 5));
			event.setArg("Term", event.getArg("Term"));
			
			event.setArg("Dir", rs_entity.sbj_id);

			sql = "UPDATE 
						t_schema 
					SET 
						cna_id = :cnaId, 
						prop_id = :propId, 
						term_index = :termIndex, 
						academic_year = :academicYear, 
						academic_term = :academicTerm 
					WHERE 
						shm_id = :shmId "; 
			queryObj = new Query( datasource=application.dnsMaster );

			queryObj.addParam( name="shmId", value=event.getArg("SchemaID"), cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="academicTerm", value=academicTermYear[2], cfsqltype="cf_sql_char" );
			queryObj.addParam( name="academicYear", value=academicTermYear[1], cfsqltype="cf_sql_char" );
			queryObj.addParam( name="termIndex", value=event.getArg("Term"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="propId", value=event.getArg("CourseProperty"), cfsqltype="cf_sql_char" );
			queryObj.addParam( name="cnaId", value=event.getArg("CourseNature"), cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
		}
		
	}
	
	/**
	* @hint "保存学费信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveGraduateCharge( required MachII.framework.Event event ) output=false {
		
		sequenceGraduateCharge = getsequenceGraduateCharge();
		
		var subject = "";
		var grade = event.getArg("Grade");
		var charge = "";
		var chargeExist = "";
		var id = "";

		/* Every SQL operation is TRANSACATION*/
		transaction {

			var sql = "	SELECT
						s.sbj_id
					FROM
						t_subject s
					WHERE
						s.sbj_id = :subjectId ";
			
			var queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="subjectId", value = event.getArg("SubjectID"), cfsqltype="cf_sql_varchar" );

			var rs_subjectEntity = queryObj.execute( sql=sql ).getResult();
			
			sequenceGraduateCharge.setSubjectId(rs_subjectEntity.sbj_id);
			sequenceGraduateCharge.setGrade( grade );
			
			id = sequenceGraduateCharge.getID();

			sql = "	SELECT
						c.chg_id
					FROM
						t_charge c
					WHERE
						c.chg_id = :chargeId ";
			
			var queryObj = new Query( datasource = application.dnsMaster );
			
			queryObj.addParam( name="chargeId", value = id, cfsqltype="cf_sql_varchar" );

			var rs_chargeExist = queryObj.execute( sql=sql ).getResult();
			
			if(rs_chargeExist.recordCount eq 0){
				sql = "INSERT INTO t_charge(
							chg_id,
							grade,
							sbj_id,
							regular,
							resit,
							secondary,
							assist,
							advanced
						)VALUES(
							:chargeId ,
							:grade ,
							:subjectId ,
							:regular ,
							:resit ,
							:secondary ,
							:assist ,
							:advanced 
						)";
				var queryObj = new Query( datasource = application.dnsMaster );
			
				queryObj.addParam( name="chargeId", value = id, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="grade", value = grade, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="subjectId", value = event.getArg("SubjectID"), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="regular", value = event.getArg('Regular', 0), cfsqltype="cf_sql_decimal", scale = 2 );
				queryObj.addParam( name="resit", value = event.getArg('Resit', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="secondary", value = event.getArg('Secondary', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="assist", value = event.getArg('Assist', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="advanced", value = event.getArg('Advanced', 0), cfsqltype="cf_sql_varchar", scale = 2 );
	
				queryObj.execute( sql=sql );
			} else {
				sql = "	UPDATE t_charge t
						   SET t.regular = :regular ,
							   t.resit = :resit ,
							   t.secondary = :secondary ,
							   t.assist = :assist ,
							   t.advanced = :advanced 
						WHERE
						   t.chg_id = :chargeId ";
				var queryObj = new Query( datasource = application.dnsMaster );
			
				queryObj.addParam( name="chargeId", value = id, cfsqltype="cf_sql_varchar" );
			
				queryObj.addParam( name="regular", value = event.getArg('Regular', 0), cfsqltype="cf_sql_decimal", scale = 2 );
				queryObj.addParam( name="resit", value = event.getArg('Resit', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="secondary", value = event.getArg('Secondary', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="assist", value = event.getArg('Assist', 0), cfsqltype="cf_sql_varchar", scale = 2 );
				queryObj.addParam( name="advanced", value = event.getArg('Advanced', 0), cfsqltype="cf_sql_varchar", scale = 2 );
	
				queryObj.execute( sql=sql );
			}

			/* Pass Args */
			event.setArg("Schema", grade & rs_subjectEntity.sbj_id);
			event.setArg("Edition", grade);
			event.setArg("SubID", rs_subjectEntity.sbj_id);
			event.setArg("TabID", "labCharge");
		}
		
	}
	
	/**
	* @hint "保存学习学分修读进度信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveGraduateProgress( required MachII.framework.Event event ) output=false {
		
		var sequenceGraduateProgress = getsequenceGraduateProgress();
		
		var subject = "";
		var grade = event.getArg("Grade");
		var progress = "";
		var progressExist = "";
		var progressLow = "";
		var direction = "";
		var id = "";
		
		var progressAdvice = getgraduateProgressAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sql = "	SELECT
							s.sbj_id,s.schooling_length
						FROM
							t_subject s
						WHERE
							s.sbj_id = :subjectId ";

			var queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name = "subjectId", value = event.getArg("SubjectID"), cfsqltype = "cf_sql_varchar" );
			
    		var rs_subjectEntity = queryObj.execute( sql=sql ).getResult();
			
			sql = "	SELECT
						s.sbj_id,s.schooling_length
					FROM
						t_subject s
					WHERE
						s.sbj_id LIKE :parent 
					ORDER BY
						s.sbj_id ASC";
			
			queryObj = new Query( datasource = application.dnsMaster );
			
			queryObj.addParam( name = "parent", value = rs_subjectEntity.sbj_id & '_', cfsqltype = "cf_sql_varchar" );
			
    		var rs_dir = queryObj.execute( sql=sql ).getResult();
			

			

			if( rs_dir.recordCount ) {
				/* 如果有专业方向 为了在未分方向前正确执行学籍预警 需要设置一个专业的最低学分 */

				sequenceGraduateProgress.setGrade(grade);
				sequenceGraduateProgress.setSubjectId(rs_subjectEntity.sbj_id);
				
				var id = sequenceGraduateProgress.getID();
				
				sql = "	SELECT
							p.pgs_id
						FROM
							t_progress p
						WHERE
							p.pgs_id = :id ";
							
				queryObj = new Query( datasource=application.dnsMaster );
			
				queryObj.addParam( name = "id", value = id, cfsqltype = "cf_sql_varchar" );
			
    			var rs_progressExist = queryObj.execute( sql=sql ).getResult();
				
				
				for(var a = 1; a LTE rs_dir.recordCount; a++){
					/* 转换学分信息存储格式 */
					for (var i=1; i lte 2 * rs_subjectEntity.schooling_length; i++) {
						if (event.isArgDefined( "A" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] )) {
							if ( progressAdvice.getCreditByTerm(i) eq 0 or progressAdvice.getCreditByTerm(i) gt event.getArg( "A" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a]) ) {
								progressAdvice.setCreditByTerm(i, event.getArg( "A" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] ));
							}
						}
						if (event.isArgDefined( "R" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] )) {
							if ( progressAdvice.getCreditRequiredByTerm(i) eq 0 or progressAdvice.getCreditRequiredByTerm(i) gt event.getArg( "R" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a]) ) {
								progressAdvice.setCreditRequiredByTerm(i, event.getArg( "R" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] ));
							}
						}
				
					}
				}
				
				if( rs_progressExist.recordCount eq 0 ){

					sql = "INSERT INTO t_progress(
								pgs_id,
								grade,
								sbj_id,
								credit_required,
								credit_all
							)VALUES(
								:progressId ,
								:grade ,
								:subjectId ,
								:creditRequired ,
								:creditAll 
							)";
					
					queryObj = new Query( datasource = application.dnsMaster );

					queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "grade", value = grade, cfsqltype = "cf_sql_char" );
					queryObj.addParam( name = "subjectId", value = rs_subjectEntity.sbj_id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );

					queryObj.execute( sql = sql );
					
				} else {
					sql = "	UPDATE t_progress t
							   SET t.credit_required = :creditRequired ,
								   t.credit_all = :creditAll 
							   WHERE
								   t.pgs_id = :progressId ";
								   
					queryObj = new Query( datasource = application.dnsMaster );

					queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );

					queryObj.execute( sql = sql );   
				}
				
				for(var a = 1; a LTE rs_dir.recordCount; a++){
					
					sequenceGraduateProgress = getsequenceGraduateProgress();
					
					sequenceGraduateProgress.setGrade(grade);
					sequenceGraduateProgress.setSubjectId(rs_dir['sbj_id'][a]);
					
					id = sequenceGraduateProgress.getID();
					
					sql = "	SELECT
							p.pgs_id
						FROM
							t_progress p
						WHERE
							p.pgs_id = :id ";
								
					queryObj = new Query( datasource=application.dnsMaster );
				
					queryObj.addParam( name = "id", value = id, cfsqltype = "cf_sql_varchar" );
				
					rs_progressExist = queryObj.execute( sql=sql ).getResult();
					
					
					/* 转换学分信息存储格式 */
					for (var i=1; i lte 2 * rs_subjectEntity.schooling_length; i++) {
				
						if (event.isArgDefined( "A" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] )) {
							progressAdvice.setCreditByTerm(i, event.getArg( "A" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] ));
						}
						if (event.isArgDefined( "R" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] )) {
							progressAdvice.setCreditRequiredByTerm(i, event.getArg( "R" & numberFormat(i, '0_') & "D" & rs_dir['sbj_id'][a] ));
						}
				
					}
					
					if(rs_progressExist.recordCount eq 0){
						sql = "INSERT INTO t_progress(
								pgs_id,
								grade,
								sbj_id,
								credit_required,
								credit_all
							)VALUES(
								:progressId ,
								:grade ,
								:subjectId ,
								:creditRequired ,
								:creditAll 
							)";
					
						queryObj = new Query( datasource = application.dnsMaster );
	
						queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
						queryObj.addParam( name = "grade", value = grade, cfsqltype = "cf_sql_char" );
						queryObj.addParam( name = "subjectId", value = rs_dir['sbj_id'][a], cfsqltype = "cf_sql_varchar" );
						queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
						queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
	
						queryObj.execute( sql = sql );
					}
					else {
						sql = "	UPDATE t_progress t
							   SET t.credit_required = :creditRequired ,
								   t.credit_all = :creditAll 
							   WHERE
								   t.pgs_id = :progressId ";

						queryObj = new Query( datasource = application.dnsMaster );
	
						queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
						queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
						queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
	
						queryObj.execute( sql = sql );
					}

				}
				
			} else {
				
				/*只有专业 没有方向*/
				sequenceGraduateProgress = getsequenceGraduateProgress();
					
				sequenceGraduateProgress.setGrade(grade);
				sequenceGraduateProgress.setSubjectId(rs_subjectEntity.sbj_id);
			
				id = sequenceGraduateProgress.getID();

				sql = "	SELECT
							p.pgs_id
						FROM
							t_progress p
						WHERE
							p.pgs_id = :id ";
								
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name = "id", value = id, cfsqltype = "cf_sql_varchar" );
				
				rs_progressExist = queryObj.execute( sql=sql ).getResult();
			
				/* 转换学分信息存储格式 */
				for (var i=1; i lte 2 * rs_subjectEntity.schooling_length; i++) {
					if (event.isArgDefined("A"&numberFormat(i, '0_'))) {
						progressAdvice.setCreditByTerm(i, event.getArg("A"&numberFormat(i, '0_')));
					}
					if (event.isArgDefined("R"&numberFormat(i, '0_'))) {
						progressAdvice.setCreditRequiredByTerm(i, event.getArg("R"&numberFormat(i, '0_')));
					}
				}

				if(rs_progressExist.recordCount eq 0){
					sql = "INSERT INTO t_progress(
								pgs_id,
								grade,
								sbj_id,
								credit_required,
								credit_all
							)VALUES(
								:progressId ,
								:grade ,
								:subjectId ,
								:creditRequired ,
								:creditAll 
							)";
					
					queryObj = new Query( datasource = application.dnsMaster );
	
					queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "grade", value = grade, cfsqltype = "cf_sql_char" );
					queryObj.addParam( name = "subjectId", value = rs_subjectEntity.sbj_id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );

					queryObj.execute( sql = sql );

				}
				else {
					sql = "	UPDATE t_progress t
							   SET t.credit_required = :creditRequired ,
								   t.credit_all = :creditAll 
							   WHERE
								   t.pgs_id = :progressId ";

					queryObj = new Query( datasource = application.dnsMaster );
	
					queryObj.addParam( name = "progressId", value = id, cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditRequired", value = progressAdvice.getCreditRequired( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
					queryObj.addParam( name = "creditAll", value = progressAdvice.getCredit( rs_subjectEntity.schooling_length ), cfsqltype = "cf_sql_varchar" );
	
					queryObj.execute( sql = sql );
					
				}
			}

			/* Pass Args */
			event.setArg("Schema", grade & rs_subjectEntity.sbj_id);
			event.setArg("Edition", grade);
			event.setArg("SubID", rs_subjectEntity.sbj_id);
			
		}
		
	}
	
	/**
	* @hint "保存毕业条件信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function saveGraduateCondition( required MachII.framework.Event event ) output=false {
		var sequenceGraduateCondition = getsequenceGraduateCondition();

		var grade = event.getArg("Grade");
		var subjectId = event.getArg("SubjectID");
		var foundDate = "";
		var id = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sequenceGraduateCondition.setGrade(grade);
			sequenceGraduateCondition.setSubjectID(subjectId);
			
			id = sequenceGraduateCondition.getID();
			
			sql = "SELECT term_start 
					  FROM t_subject 
					  WHERE sbj_id = :subjectId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="subjectId", value=subjectId, cfsqltype="cf_sql_varchar" );
			rs_subject = queryObj.execute( sql=sql ).getResult();

			sql = "SELECT 
						credit, credit_required, credit_optional, credit_relish, 
						period, period_required, period_optional, grade 
					FROM 
						t_condition 
					WHERE 
						con_id = :conId "; 
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="conId", value=id, cfsqltype="cf_sql_varchar" );
			rs_conditionExist = queryObj.execute( sql=sql ).getResult();
	
			/* 秋季招生 则建班为9月 春季招生为3月 */
			if (rs_subject.term_start == "1") {
				foundDate = grade & "09";
			} 
			else {
				foundDate = grade & "03";
			}
			
			if (rs_conditionExist.recordCount EQ 0) {
				sql = "INSERT INTO 
						  t_condition(
						  		con_id,
								grade,
								sbj_id,
								credit,
								credit_required,
								credit_optional,
								credit_relish,
								period,
								period_required,
								period_optional,
								found_date 
							) 
							VALUES(
								:conId, 
								:grade, 
								:sbjId, 
								:credit, 
								:creditRequired,
								:creditOptional, 
								:creditRelish, 
								:period, 
								:periodRequired, 
								:periodOptional, 
								:foundDate 
							) ";
								
				queryObj = new Query( datasource=application.dnsMaster );
			
				queryObj.addParam( name="conId", value=id, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="grade", value=grade, cfsqltype="cf_sql_char" );
				queryObj.addParam( name="sbjId", value=subjectId, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="credit", value=event.getArg("Credit"), cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRequired", value=event.getArg("CreditRequired"), cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditOptional", value=event.getArg("CreditOptional"), cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRelish", value=event.getArg("CreditRelish"), cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="period", value=event.getArg("Period"), cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodRequired", value=event.getArg("PeriodRequired"), cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodOptional", value=event.getArg("PeriodOptional"), cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="foundDate", value=foundDate, cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );
			}
			else {
				grade = rs_conditionExist.grade;
				
				var credit = "";
				var creditRequired = "";
				var creditOptional = "";
				var creditRelish = "";
				var period = "";
				var periodRequired = "";
				var periodOptional = "";
				
				/* 只有更新有效条件 */
				if ( event.getArg("Credit") gte 0 ) { 
					credit = event.getArg("Credit");
				}else{
					credit = rs_conditionExist.credit;
				}
				
				if ( event.getArg("CreditRequired") gte 0 ) { 
					creditRequired = event.getArg("CreditRequired"); 
				}else{
					creditRequired = rs_conditionExist.credit_required;
				}
				
				if ( event.getArg("CreditOptional") gte 0 ) { 
					creditOptional = event.getArg("CreditOptional"); 
				}else{
					creditOptional = rs_conditionExist.credit_optional;
				}
				
				if ( event.getArg("CreditRelish") gte 0 ) { 
					creditRelish = event.getArg("CreditRelish"); 
				}else{
					creditRelish = rs_conditionExist.credit_relish;
				}
				
				if ( event.getArg("Period") gte 0 ) { 
					period = event.getArg("Period"); 
				}else{
					period = rs_conditionExist.period;
				}
				
				if ( event.getArg("PeriodRequired") gte 0 ) { 
					periodRequired = event.getArg("PeriodRequired"); 
				}else{
					periodRequired = rs_conditionExist.period_required;
				}
				
				if ( event.getArg("PeriodOptional") gte 0 ) { 
					periodOptional = event.getArg("PeriodOptional"); 
				}else{
					periodOptional = rs_conditionExist.period_optional;
				}
				
				sql = "UPDATE t_condition
						SET 
							credit = :credit,
							credit_required = :creditRequired,
							credit_optional = :creditOptional,
							credit_relish = :creditRelish,
							period = :period,
							period_required = :periodRequired,
							period_optional = :periodOptional,
							found_date = :foundDate 
						WHERE  
							con_id = :conId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
			
				queryObj.addParam( name="conId", value=id, cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="credit", value=credit, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRequired", value=creditRequired, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditOptional", value=creditOptional, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="creditRelish", value=creditRelish, cfsqltype="cf_sql_decimal", scale=1 );
				queryObj.addParam( name="period", value=period, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodRequired", value=periodRequired, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="periodOptional", value=periodOptional, cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="foundDate", value=foundDate, cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );

			}

			/* Pass Args */
			event.setArg("Schema", id);
			event.setArg("Edition", grade );
			event.setArg("SubID", subjectId);

		}	
	}
	
}

</cfscript>

